package com.myGisChannel.controller;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.myGis.entities.Layer;
import com.myGis.entities.Project;
import com.myGis.entities.User;
import com.myGis.persistence.IPersistenceData;
import com.myGis.persistence.PersistenceData;
import com.myGis.services.IMyGisServices;
import com.myGis.services.MyGisServer;
import com.myGis.util.HibernateUtil;
import com.myGisChannel.form.MultiPartFileForm;
 

 
@Controller
@RequestMapping("greet")
public class GreetController {
 
     
 
    @RequestMapping(method = RequestMethod.GET)
    public @ModelAttribute("fileFormBean") MultiPartFileForm getInitialMessage() {
        return new MultiPartFileForm();
    }
 
    @RequestMapping(method = RequestMethod.POST)
    public @ModelAttribute("message") String guardaFichero(@ModelAttribute MultiPartFileForm fileFormBean) {
         
        try {
            grabarFicheroALocal(fileFormBean);
        } catch (Exception e) {
            e.printStackTrace();
            return "No se ha podido grabar el fichero";
        }
         
        return "Fichero grabado correctamente";
    }
 
    private void grabarFicheroALocal(MultiPartFileForm fileFormBean) throws Exception {
        CommonsMultipartFile uploaded = fileFormBean.getFichero();
        File localFile = new File("C:/Program Files/apache-tomcat-5.5.29/work/Catalina/localhost/myGisChannel/"+uploaded.getOriginalFilename());
        //File file = new File(uploaded.getStorageDescription());
        FileOutputStream os = null;
         
        try {
             
            	os = new FileOutputStream(localFile);
            	os.write(uploaded.getBytes());
            	// --- TODO Reajustar esto
	    		Session session = HibernateUtil.getSessionFactory().openSession();
	    		IPersistenceData ipd = new PersistenceData(session);
	
	    		User usuario = new User();
	    		usuario.setUserName("Josesito");
	    		usuario.setPassword("pakapaka");
	    		ipd.save(usuario);
	
	    		Project proyecto = new Project();
	    		proyecto.setName("TestProyecto");
	    		proyecto.setLatitudeInitial(1.0d);
	    		proyecto.setLongitudeInitial(1.0d);
	    		proyecto.getUsers().add(usuario);
	    		ipd.save(proyecto);
	
	    		Layer layer = new Layer();
	    		layer.setName("testLayer");
	    		proyecto.getLayers().add(layer);
	
	    		ipd.save(proyecto);
	    		
	    		
	    		
	    		IMyGisServices ims = new MyGisServer(session);
	    		
	    		//File file = new File("src/resources/Electricidad 2011.shp");
	    		/*
	    		try {
	    			ims.shapeToDB(localFile,layer);
	    		} catch (Exception e) {
	    			e.printStackTrace();
	    		}
	*/
	    		HibernateUtil.shutdown();
            // --- Fin reajustar
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}